<?php

define('IN_API', true);
require_once '../../framework/bootstrap.inc.php';
require_once 'defines.php';
$id = $_GPC['id'];
$_W['uniacid'] = $id;
unset($_GPC['id']);
if($_GPC['__input']){
	$post = $_GPC['__input'];
}else{
	$post = $_GPC;
}

$log = pdo_insert(MODULE_NAME . "_systemlog", array("uniacid" => $id, "api_appKey" => $post['appKey'],"apiname" => $post['eventName'],"create_time" => $_W["timestamp"], "log" => json_encode($_GPC)));
if($post['ticketCode']){
	$post['ticketCode'] = str_replace('&quot;','"',$post['ticketCode']);
}
if($post['ticketImage']){
	$post['ticketImage'] = str_replace('&quot;','"',$post['ticketImage']);
}
$settings = pdo_get("uni_account_modules", array("module" => MODULE_NAME , "uniacid" => $id));
$settings = unserialize($settings['settings']);
if(!$settings['movie_api_s_appKey'] || !$settings['movie_api_s_appSecret']){
	$log = pdo_insert(MODULE_NAME . "_systemlog", array("uniacid" => $id, "api_appKey" => $post['appKey'],"apiname" => $post['eventName'],"create_time" => $_W["timestamp"], "log" => 101));
	exit(json_encode(array('success' => false, 'code' => 101, 'message' => '系统错误')));
}
$sign = get_sign($post, $settings['movie_api_s_appSecret']);
$api_url = $settings['movie_api_s_url'];
if($sign != $post['sign']){
	$log = pdo_insert(MODULE_NAME . "_systemlog", array("uniacid" => $id, "api_appKey" => $post['appKey'],"apiname" => $post['eventName'],"create_time" => $_W["timestamp"], "log" => 102));
	exit(json_encode(array('success' => false, 'code' => 102, 'message' => '系统错误')));
}

$eventName = $post['eventName'];
$order = pdo_get(MODULE_NAME . "_movieorder", array("thirdOrderId" => $post['thirdOrderId']));
if(!$order){
	$log = pdo_insert(MODULE_NAME . "_systemlog", array("uniacid" => $id, "api_appKey" => $post['appKey'],"apiname" => $post['eventName'],"create_time" => $_W["timestamp"], "log" => 103));
	exit(json_encode(array('success' => false, 'code' => 103, 'message' => '系统错误')));
}
$tidarr = explode("_", $order['thirdOrderId']);
$order_id = $tidarr[2];
switch ($eventName) {

	case "ORDER_CLOSE":
		$res=pdo_update(MODULE_NAME . '_movieorder', array('orderStatus' => 10,'orderStatusStr' => '订单关闭','closeCause' => $post['closeCause']), array('id' => $order['id']));
	    //退款
		load()->model('refund');
		if($order['seatId']){
			$refund_id = refund_create_order("movie2_" . $order["userOrderId"], MODULE_NAME . '', floatval(round($order["orderPrice"]/100,2)), '电影票退款');
		}else{
			$refund_id = refund_create_order("movie_" . $order["userOrderId"], MODULE_NAME . '', floatval(round($order["orderPrice"]/100,2)), '电影票退款');
		}
		
		if (is_error($refund_id)) {
			$log = pdo_insert(MODULE_NAME . "_systemlog", array("uniacid" => $id, "api_appKey" => $post['appKey'],"apiname" => $post['eventName'],"create_time" => $_W["timestamp"], "log" => json_encode($refund_id)));
		}else{
			$refund_result = refund1($refund_id);
			if (is_error($refund_result)) {
				$log = pdo_insert(MODULE_NAME . "_systemlog", array("uniacid" => $id, "api_appKey" => $post['appKey'],"apiname" => $post['eventName'],"create_time" => $_W["timestamp"], "log" => json_encode($refund_result)));
			} else {
				pdo_update('core_refundlog', array('status' => 1), array('id' => $refund_id));
				//更新总订单表为关闭状态
				if($settings['system_type'] == 1){
					$resin = pdo_update(MODULE_NAME . "_order_u", array("order_profit" => 0, "order_status" => 9 , "c_1" => 0 , "c_2" => 0 , "c_3" => 0 , "c_t" => 0, "c_2t" => 0, "c_1h" => 0, "c_2h" => 0, "c_3h" => 0, "c_4h" => 0, "c_1_uid" => 0, "c_2_uid" => 0, "c_3_uid" => 0, "c_t_uid" => 0, "c_2t_uid" => 0, "c_1h_uid" => 0 , "c_2h_uid" => 0 , "c_3h_uid" => 0 , "c_4h_uid" => 0) , array("order_id" => $order_id , "system_id" => $settings['system_id']));
				}else{
					$resin = pdo_update(MODULE_NAME . "_order", array("order_profit" => 0, "order_status" => 9 , "c_1" => 0 , "c_2" => 0 , "c_3" => 0 , "c_t" => 0, "c_2t" => 0, "c_1h" => 0, "c_2h" => 0, "c_3h" => 0, "c_4h" => 0, "c_1_uid" => 0, "c_2_uid" => 0, "c_3_uid" => 0, "c_t_uid" => 0, "c_2t_uid" => 0, "c_1h_uid" => 0 , "c_2h_uid" => 0 , "c_3h_uid" => 0 , "c_4h_uid" => 0) , array("order_id" => $order_id , "uniacid" => $id));
				}
				//更新总订单表为结算状态
				$res = xcxmovierefundNotify($order,$id);
			}
		}
		exit(json_encode(array('success' => true, 'code' => 200, 'message' => '请求成功')));
	case "WAIT_TICKET":
		$res=pdo_update(MODULE_NAME . '_movieorder', array('orderStatus' => 3,'orderStatusStr' => '待出票','realorderPrice' => $post['orderPrice'],'callbacktime' => $post['time']), array('id' => $order['id']));
		exit(json_encode(array('success' => true, 'code' => 200, 'message' => '请求成功')));
	case "TICKET_SUCCESS":
		$res=pdo_update(MODULE_NAME . '_movieorder', array('orderStatus' => 4,'orderStatusStr' => '已出票待结算','realSeat' => $post['realSeat'],'ticketCode' => $post['ticketCode'],'ticketImage' => $post['ticketImage']), array('id' => $order['id']));
	    $order['realSeat'] = $post['realSeat'];
	    $res = xcxmoviesuccessNotify($order,$id);
		//计算总订单表佣金
		if($settings['system_type'] == 1){
			$u = pdo_get(MODULE_NAME . "_user_u", array("unionid" => $order["unionid"], "system_id" => $settings['system_id']));
			$fee = fee($u['uid'],$id);
			$order_profit = round(($order['orderPrice'] - $order['realorderPrice'])/100,2);
			if($order_profit <= 0){
				$resin = pdo_update(MODULE_NAME . "_order_u", array("order_profit" => 0 , "c_1" => 0 , "c_2" => 0 , "c_3" => 0 , "c_t" => 0, "c_2t" => 0, "c_1h" => 0, "c_2h" => 0, "c_3h" => 0, "c_4h" => 0, "c_1_uid" => 0, "c_2_uid" => 0, "c_3_uid" => 0, "c_t_uid" => 0, "c_2t_uid" => 0, "c_1h_uid" => 0 , "c_2h_uid" => 0 , "c_3h_uid" => 0 , "c_4h_uid" => 0) , array("order_id" => $order_id , "system_id" => $settings['system_id']));
			}else{
				$resin = pdo_update(MODULE_NAME . "_order_u", array("order_profit" => $order_profit , "c_1" => round($order_profit * $fee['c_1_fee'] * $settings['movie_yjrate']/10000,2) , "c_2" => round($order_profit * $fee['c_2_fee'] * $settings['movie_yjrate']/10000,2) , "c_3" => round($order_profit * $fee['c_3_fee'] * $settings['movie_yjrate']/10000,2) , "c_t" => round($order_profit * $fee['c_t_fee'] * $settings['movie_yjrate']/10000,2), "c_2t" => round($order_profit * $fee['c_2t_fee'] * $settings['movie_yjrate']/10000,2), "c_1h" => round($order_profit * $fee['c_1h_fee'] * $settings['movie_yjrate']/10000,2), "c_2h" => round($order_profit * $fee['c_2h_fee'] * $settings['movie_yjrate']/10000,2), "c_3h" => round($order_profit * $fee['c_3h_fee'] * $settings['movie_yjrate']/10000,2), "c_4h" => round($order_profit * $fee['c_4h_fee'] * $settings['movie_yjrate']/10000,2), "c_1_uid" => $fee['c_1_uid'], "c_2_uid" => $fee['c_2_uid'], "c_3_uid" => $fee['c_3_uid'], "c_t_uid" => $fee['c_t_uid'], "c_2t_uid" => $fee['c_2t_uid'], "c_1h_uid" => $fee['c_1h_uid'] , "c_2h_uid" => $fee['c_2h_uid'] , "c_3h_uid" => $fee['c_3h_uid'] , "c_4h_uid" => $fee['c_4h_uid']) , array("order_id" => $order_id , "system_id" => $settings['system_id']));
			}
			
		}else{
			$u = pdo_get(MODULE_NAME . "_user", array("openid" => $order["openid"], "uniacid" => $id));
			$fee = fee($u['uid'],$id);
			$order_profit = round(($order['orderPrice'] - $order['realorderPrice'])/100,2);
			if($order_profit <= 0){
				$resin = pdo_update(MODULE_NAME . "_order", array("order_profit" => 0 , "c_1" => 0 , "c_2" => 0 , "c_3" => 0 , "c_t" => 0, "c_2t" => 0, "c_1h" => 0, "c_2h" => 0, "c_3h" => 0, "c_4h" => 0, "c_1_uid" => 0, "c_2_uid" => 0, "c_3_uid" => 0, "c_t_uid" => 0, "c_2t_uid" => 0, "c_1h_uid" => 0 , "c_2h_uid" => 0 , "c_3h_uid" => 0 , "c_4h_uid" => 0) , array("order_id" => $order_id , "uniacid" => $id));
			}else{
				$resin = pdo_update(MODULE_NAME . "_order", array("order_profit" => $order_profit , "c_1" => round($order_profit * $fee['c_1_fee'] * $settings['movie_yjrate']/10000,2) , "c_2" => round($order_profit * $fee['c_2_fee'] * $settings['movie_yjrate']/10000,2) , "c_3" => round($order_profit * $fee['c_3_fee'] * $settings['movie_yjrate']/10000,2) , "c_t" => round($order_profit * $fee['c_t_fee'] * $settings['movie_yjrate']/10000,2), "c_2t" => round($order_profit * $fee['c_2t_fee'] * $settings['movie_yjrate']/10000,2), "c_1h" => round($order_profit * $fee['c_1h_fee'] * $settings['movie_yjrate']/10000,2), "c_2h" => round($order_profit * $fee['c_2h_fee'] * $settings['movie_yjrate']/10000,2), "c_3h" => round($order_profit * $fee['c_3h_fee'] * $settings['movie_yjrate']/10000,2), "c_4h" => round($order_profit * $fee['c_4h_fee'] * $settings['movie_yjrate']/10000,2), "c_1_uid" => $fee['c_1_uid'], "c_2_uid" => $fee['c_2_uid'], "c_3_uid" => $fee['c_3_uid'], "c_t_uid" => $fee['c_t_uid'], "c_2t_uid" => $fee['c_2t_uid'], "c_1h_uid" => $fee['c_1h_uid'] , "c_2h_uid" => $fee['c_2h_uid'] , "c_3h_uid" => $fee['c_3h_uid'] , "c_4h_uid" => $fee['c_4h_uid']) , array("order_id" => $order_id , "uniacid" => $id));
			}
			
		}
		//计算总订单表佣金
		exit(json_encode(array('success' => true, 'code' => 200, 'message' => '请求成功')));
	case "TICKET_SYNC"://参数与TICKET_SUCCESS一致
		$res=pdo_update(MODULE_NAME . '_movieorder', array('orderStatus' => 4,'orderStatusStr' => '已出票待结算（取票信息修改）','realSeat' => $post['realSeat'],'ticketCode' => $post['ticketCode'],'ticketImage' => $post['ticketImage']), array('id' => $order['id']));
		$order['realSeat'] = $post['realSeat'];
		$res = xcxmoviesuccessNotify($order,$id);
		//计算总订单表佣金
		if($settings['system_type'] == 1){
			$u = pdo_get(MODULE_NAME . "_user_u", array("unionid" => $order["unionid"], "system_id" => $settings['system_id']));
			$fee = fee($u['uid'],$id);
			$order_profit = round(($order['orderPrice'] - $order['realorderPrice'])/100,2);
			if($order_profit <= 0){
				$resin = pdo_update(MODULE_NAME . "_order_u", array("order_profit" => 0 , "c_1" => 0 , "c_2" => 0 , "c_3" => 0 , "c_t" => 0, "c_2t" => 0, "c_1h" => 0, "c_2h" => 0, "c_3h" => 0, "c_4h" => 0, "c_1_uid" => 0, "c_2_uid" => 0, "c_3_uid" => 0, "c_t_uid" => 0, "c_2t_uid" => 0, "c_1h_uid" => 0 , "c_2h_uid" => 0 , "c_3h_uid" => 0 , "c_4h_uid" => 0) , array("order_id" => $order_id , "system_id" => $settings['system_id']));
			}else{
				$resin = pdo_update(MODULE_NAME . "_order_u", array("order_profit" => $order_profit , "c_1" => round($order_profit * $fee['c_1_fee'] * $settings['movie_yjrate']/10000,2) , "c_2" => round($order_profit * $fee['c_2_fee'] * $settings['movie_yjrate']/10000,2) , "c_3" => round($order_profit * $fee['c_3_fee'] * $settings['movie_yjrate']/10000,2) , "c_t" => round($order_profit * $fee['c_t_fee'] * $settings['movie_yjrate']/10000,2), "c_2t" => round($order_profit * $fee['c_2t_fee'] * $settings['movie_yjrate']/10000,2), "c_1h" => round($order_profit * $fee['c_1h_fee'] * $settings['movie_yjrate']/10000,2), "c_2h" => round($order_profit * $fee['c_2h_fee'] * $settings['movie_yjrate']/10000,2), "c_3h" => round($order_profit * $fee['c_3h_fee'] * $settings['movie_yjrate']/10000,2), "c_4h" => round($order_profit * $fee['c_4h_fee'] * $settings['movie_yjrate']/10000,2), "c_1_uid" => $fee['c_1_uid'], "c_2_uid" => $fee['c_2_uid'], "c_3_uid" => $fee['c_3_uid'], "c_t_uid" => $fee['c_t_uid'], "c_2t_uid" => $fee['c_2t_uid'], "c_1h_uid" => $fee['c_1h_uid'] , "c_2h_uid" => $fee['c_2h_uid'] , "c_3h_uid" => $fee['c_3h_uid'] , "c_4h_uid" => $fee['c_4h_uid']) , array("order_id" => $order_id , "system_id" => $settings['system_id']));
			}
			
		}else{
			$u = pdo_get(MODULE_NAME . "_user", array("openid" => $order["openid"], "uniacid" => $id));
			$fee = fee($u['uid'],$id);
			$order_profit = round(($order['orderPrice'] - $order['realorderPrice'])/100,2);
			if($order_profit <= 0){
				$resin = pdo_update(MODULE_NAME . "_order", array("order_profit" => 0 , "c_1" => 0 , "c_2" => 0 , "c_3" => 0 , "c_t" => 0, "c_2t" => 0, "c_1h" => 0, "c_2h" => 0, "c_3h" => 0, "c_4h" => 0, "c_1_uid" => 0, "c_2_uid" => 0, "c_3_uid" => 0, "c_t_uid" => 0, "c_2t_uid" => 0, "c_1h_uid" => 0 , "c_2h_uid" => 0 , "c_3h_uid" => 0 , "c_4h_uid" => 0) , array("order_id" => $order_id , "uniacid" => $id));
			}else{
				$resin = pdo_update(MODULE_NAME . "_order", array("order_profit" => $order_profit , "c_1" => round($order_profit * $fee['c_1_fee'] * $settings['movie_yjrate']/10000,2) , "c_2" => round($order_profit * $fee['c_2_fee'] * $settings['movie_yjrate']/10000,2) , "c_3" => round($order_profit * $fee['c_3_fee'] * $settings['movie_yjrate']/10000,2) , "c_t" => round($order_profit * $fee['c_t_fee'] * $settings['movie_yjrate']/10000,2), "c_2t" => round($order_profit * $fee['c_2t_fee'] * $settings['movie_yjrate']/10000,2), "c_1h" => round($order_profit * $fee['c_1h_fee'] * $settings['movie_yjrate']/10000,2), "c_2h" => round($order_profit * $fee['c_2h_fee'] * $settings['movie_yjrate']/10000,2), "c_3h" => round($order_profit * $fee['c_3h_fee'] * $settings['movie_yjrate']/10000,2), "c_4h" => round($order_profit * $fee['c_4h_fee'] * $settings['movie_yjrate']/10000,2), "c_1_uid" => $fee['c_1_uid'], "c_2_uid" => $fee['c_2_uid'], "c_3_uid" => $fee['c_3_uid'], "c_t_uid" => $fee['c_t_uid'], "c_2t_uid" => $fee['c_2t_uid'], "c_1h_uid" => $fee['c_1h_uid'] , "c_2h_uid" => $fee['c_2h_uid'] , "c_3h_uid" => $fee['c_3h_uid'] , "c_4h_uid" => $fee['c_4h_uid']) , array("order_id" => $order_id , "uniacid" => $id));
			}
			
		}
		//计算总订单表佣金
		exit(json_encode(array('success' => true, 'code' => 200, 'message' => '请求成功')));
	case "ORDER_FINISH":
		$res=pdo_update(MODULE_NAME . '_movieorder', array('orderStatus' => 5,'orderStatusStr' => '已结算'), array('id' => $order['id']));
	    //更新总订单表为结算状态
		if($settings['system_type'] == 1){
			$u = pdo_get(MODULE_NAME . "_user_u", array("unionid" => $order["unionid"], "system_id" => $settings['system_id']));
			$fee = fee($u['uid'],$id);
			$order_profit = round(($order['orderPrice'] - $order['realorderPrice'])/100,2);
			if($order_profit <= 0){
				$resin = pdo_update(MODULE_NAME . "_order_u", array("order_profit" => 0, "order_status" => 8 , "c_1" => 0 , "c_2" => 0 , "c_3" => 0 , "c_t" => 0, "c_2t" => 0, "c_1h" => 0, "c_2h" => 0, "c_3h" => 0, "c_4h" => 0, "c_1_uid" => 0, "c_2_uid" => 0, "c_3_uid" => 0, "c_t_uid" => 0, "c_2t_uid" => 0, "c_1h_uid" => 0 , "c_2h_uid" => 0 , "c_3h_uid" => 0 , "c_4h_uid" => 0) , array("order_id" => $order_id , "system_id" => $settings['system_id']));
			}else{
				$resin = pdo_update(MODULE_NAME . "_order_u", array("order_profit" => $order_profit, "order_status" => 8 , "c_1" => round($order_profit * $fee['c_1_fee'] * $settings['movie_yjrate']/10000,2) , "c_2" => round($order_profit * $fee['c_2_fee'] * $settings['movie_yjrate']/10000,2) , "c_3" => round($order_profit * $fee['c_3_fee'] * $settings['movie_yjrate']/10000,2) , "c_t" => round($order_profit * $fee['c_t_fee'] * $settings['movie_yjrate']/10000,2), "c_2t" => round($order_profit * $fee['c_2t_fee'] * $settings['movie_yjrate']/10000,2), "c_1h" => round($order_profit * $fee['c_1h_fee'] * $settings['movie_yjrate']/10000,2), "c_2h" => round($order_profit * $fee['c_2h_fee'] * $settings['movie_yjrate']/10000,2), "c_3h" => round($order_profit * $fee['c_3h_fee'] * $settings['movie_yjrate']/10000,2), "c_4h" => round($order_profit * $fee['c_4h_fee'] * $settings['movie_yjrate']/10000,2), "c_1_uid" => $fee['c_1_uid'], "c_2_uid" => $fee['c_2_uid'], "c_3_uid" => $fee['c_3_uid'], "c_t_uid" => $fee['c_t_uid'], "c_2t_uid" => $fee['c_2t_uid'], "c_1h_uid" => $fee['c_1h_uid'] , "c_2h_uid" => $fee['c_2h_uid'] , "c_3h_uid" => $fee['c_3h_uid'] , "c_4h_uid" => $fee['c_4h_uid']) , array("order_id" => $order_id , "system_id" => $settings['system_id']));
			}
			
		}else{
			$u = pdo_get(MODULE_NAME . "_user", array("openid" => $order["openid"], "uniacid" => $id));
			$fee = fee($u['uid'],$id);
			$order_profit = round(($order['orderPrice'] - $order['realorderPrice'])/100,2);
			if($order_profit <= 0){
				$resin = pdo_update(MODULE_NAME . "_order", array("order_profit" => 0, "order_status" => 8 , "c_1" => 0 , "c_2" => 0 , "c_3" => 0 , "c_t" => 0, "c_2t" => 0, "c_1h" => 0, "c_2h" => 0, "c_3h" => 0, "c_4h" => 0, "c_1_uid" => 0, "c_2_uid" => 0, "c_3_uid" => 0, "c_t_uid" => 0, "c_2t_uid" => 0, "c_1h_uid" => 0 , "c_2h_uid" => 0 , "c_3h_uid" => 0 , "c_4h_uid" => 0) , array("order_id" => $order_id , "uniacid" => $id));
			}else{
				$resin = pdo_update(MODULE_NAME . "_order", array("order_profit" => $order_profit, "order_status" => 8 , "c_1" => round($order_profit * $fee['c_1_fee'] * $settings['movie_yjrate']/10000,2) , "c_2" => round($order_profit * $fee['c_2_fee'] * $settings['movie_yjrate']/10000,2) , "c_3" => round($order_profit * $fee['c_3_fee'] * $settings['movie_yjrate']/10000,2) , "c_t" => round($order_profit * $fee['c_t_fee'] * $settings['movie_yjrate']/10000,2), "c_2t" => round($order_profit * $fee['c_2t_fee'] * $settings['movie_yjrate']/10000,2), "c_1h" => round($order_profit * $fee['c_1h_fee'] * $settings['movie_yjrate']/10000,2), "c_2h" => round($order_profit * $fee['c_2h_fee'] * $settings['movie_yjrate']/10000,2), "c_3h" => round($order_profit * $fee['c_3h_fee'] * $settings['movie_yjrate']/10000,2), "c_4h" => round($order_profit * $fee['c_4h_fee'] * $settings['movie_yjrate']/10000,2), "c_1_uid" => $fee['c_1_uid'], "c_2_uid" => $fee['c_2_uid'], "c_3_uid" => $fee['c_3_uid'], "c_t_uid" => $fee['c_t_uid'], "c_2t_uid" => $fee['c_2t_uid'], "c_1h_uid" => $fee['c_1h_uid'] , "c_2h_uid" => $fee['c_2h_uid'] , "c_3h_uid" => $fee['c_3h_uid'] , "c_4h_uid" => $fee['c_4h_uid']) , array("order_id" => $order_id , "uniacid" => $id));
			}
			
		}
		//更新总订单表为结算状态
		exit(json_encode(array('success' => true, 'code' => 200, 'message' => '请求成功')));
}

function get_sign($params, $secret)
	{
		$string = "";
		ksort($params);
		foreach ($params as $k => $v) {
			if ($k != "sign" && !is_array($v)) {
				$string .= $k . "=" . $v . "&";
			}
		}
		$string = trim($string, "&");
		$string = $string . "&appSecret=".$secret;
		$string = md5($string);
		return $string;
	}


function xcxmoviesuccessNotify($order,$id)
	{
		global $_GPC;
		global $_W;
		$settings = pdo_get("uni_account_modules", array("module" => MODULE_NAME . '' , "uniacid" =>$id));
		$config = unserialize($settings['settings']);
		load()->model('mc');
		if (!empty($id)) {
			$_W["account"] = uni_fetch($id);
		}
		if (empty($_W["account"])) {
			exit("initial error hash or id");
		}
		$account_api = WeAccount::create();
		$token = $account_api->getAccessToken();
		$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
		$dd = array();
		$dd['touser'] = $order['openid'];
		$dd['template_id'] = $config["movie_tpl_1"];
		$dd['page'] = 'pages/user/movieorder';
		$dd['data'] = array(
			'thing1'=>array(
				"value"=>$order['filmName']
			),
			'thing2'=>array(
				"value"=>$order['cinemaName']
			),
			'date4'=>array(
				"value"=>$order['showTime']
			),
			'thing5'=>array(
				"value"=>$order['hallName']
			),
			'thing6'=>array(
				"value"=>$order['realSeat']
			)
		);
		load()->func('communication');
		$res = ihttp_request($url,json_encode($dd));
		if($settings['errorcodeid'] == 98){
			var_dump($res);
		}
		$res1 = json_decode($res["content"],true);
		$log = pdo_insert(MODULE_NAME . "_systemlog", array("uniacid" => $id, "api_appKey" => '电影票出票通知',"apiname" => 'xcxmoviesuccessNotify',"create_time" => $_W["timestamp"], "log" => json_encode($res1)));
		if($res1['errcode'] == 0){
			return true;
		}else{
			return false;
		}
	}
function xcxmovierefundNotify($order,$id)
	{
		global $_GPC;
		global $_W;
		$settings = pdo_get("uni_account_modules", array("module" => MODULE_NAME . '' , "uniacid" =>$id));
		$config = unserialize($settings['settings']);
		load()->model('mc');
		if (!empty($id)) {
			$_W["account"] = uni_fetch($id);
		}
		if (empty($_W["account"])) {
			exit("initial error hash or id");
		}
		$account_api = WeAccount::create();
		$token = $account_api->getAccessToken();
		$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
		$dd = array();
		$dd['touser'] = $order['openid'];
		$dd['template_id'] = $config["movie_tpl_2"];
		$dd['page'] = 'pages/user/movieorder';
		$dd['data'] = array(
			'thing1'=>array(
				"value"=>'出票失败，已退款，请选择其他场次购票。'
			),
			'thing2'=>array(
				"value"=>$order['cinemaName']
			),
			'thing3'=>array(
				"value"=>$order['filmName']
			)
		);
		load()->func('communication');
		$res = ihttp_request($url,json_encode($dd));
		if($settings['errorcodeid'] == 98){
			var_dump($res);
		}
		
		$res1 = json_decode($res["content"],true);
		$log = pdo_insert(MODULE_NAME . "_systemlog", array("uniacid" => $id, "api_appKey" => '电影票退款通知',"apiname" => 'xcxmovierefundNotify',"create_time" => $_W["timestamp"], "log" => json_encode($res1)));
		if($res1['errcode'] == 0){
			return true;
		}else{
			return false;
		}
	}

function fee($uid,$id)
{
	global $_GPC;
	global $_W;
	$settings = pdo_get("uni_account_modules", array("module" => MODULE_NAME . '' , "uniacid" =>$id));
	$settings = unserialize($settings['settings']);
	if($settings['system_type'] == 1){
		$u = pdo_get(MODULE_NAME . "_user_u", array("uid" => $uid, "system_id" => $settings['system_id']));
	}else{
		$u = pdo_get(MODULE_NAME . "_user", array("uid" => $uid , "uniacid" => $id));
	}
	if($u){
		//如果是普通会员
		$t = 0;$h = 0;$h1 = 0;$h2 = 0;$h3 = 0;$h4 = 0;
		if($u['is_tuan'] == 0){
			$c_1_uid = $u['uid'];
			$c_1_openid = $u['openid'];
			$c_1_s1 = $u['s_1_num'];
			$c_1_fee = $settings['c_1'];
			if($u['invite_uid'] != '0'){
				$fa_uid_array = array_reverse(explode("_",$u['invite_uid']));
				$i1 = 1;
				foreach($fa_uid_array as $v){
					if($settings['system_type'] == 1){
						$u1 = pdo_get(MODULE_NAME . "_user_u", array("uid" => $v , "system_id" => $settings['system_id']));
					}else{
						$u1 = pdo_get(MODULE_NAME . "_user", array("uid" => $v , "uniacid" => $id));
					}
					if($u1['is_tuan'] == 0 && $i1 == 1){//如果是普通会员直推
						$c_2_uid = $u1['uid'];
						$c_2_openid = $u1['openid'];
						$c_2_s1 = $u1['s_1_num'];
						$c_2_fee = $settings['c_2'];
						$i2 = 1;
						foreach($fa_uid_array as $v2){
							if($settings['system_type'] == 1){
								$u2 = pdo_get(MODULE_NAME . "_user_u", array("uid" => $v2 , "system_id" => $settings['system_id']));
							}else{
								$u2 = pdo_get(MODULE_NAME . "_user", array("uid" => $v2 , "uniacid" => $id));
							}
							if($u2['is_tuan'] == 0 && $i2 == 2){//如果是普通会员间推
								$c_3_uid = $u2['uid'];
								$c_3_openid = $u2['openid'];
								$c_3_s1 = $u2['s_1_num'];
								$c_3_fee = $settings['c_3'];
								$i3 = 1;
								foreach($fa_uid_array as $v3){
									if($settings['system_type'] == 1 && $i3 > 2){
										$u3 = pdo_get(MODULE_NAME . "_user_u", array("uid" => $v3 , "system_id" => $settings['system_id']));
									}elseif($i3 > 2){
										$u3 = pdo_get(MODULE_NAME . "_user", array("uid" => $v3 , "uniacid" => $id));
									}
									if($u3['is_tuan'] == 1 && $t == 0 && $h == 0 && $h1 == 0 && $i3 > 2){//团长非直属
										$c_t_uid = $u3['uid'];
										$c_t_openid = $u3['openid'];
										$c_t_s1 = $u3['s_1_num'];
										$c_t_fee = $settings['c_t'];
										$t = 1;
										continue;
									}elseif($u3['is_tuan'] == 1 && $t == 1 && $h == 0 && $h1 == 0  && $i3 > 2){//团长直属团长
										$c_2t_uid = $u3['uid'];
										$c_2t_openid = $u3['openid'];
										$c_2t_s1 = $u3['s_1_num'];
										$c_2t_fee = $settings['c_2t'];
										$t = 2;
										continue;
									}elseif($u3['is_tuan'] == 2 && $t == 0 && $h == 0 && $h1 == 0 && $i3 > 2){//合伙人非直属会员
										$c_t_uid = $u3['uid'];
										$c_t_openid = $u3['openid'];
										$c_t_s1 = $u3['s_1_num'];
										$c_t_fee = $settings['c_h_3'];
										$h = 2;
										continue;
									}elseif($u3['is_tuan'] == 2 && $t > 0 && $h == 0 && $h1 == 0 && $i3 > 2){//合伙人直属团队
										$c_1h_uid = $u3['uid'];
										$c_1h_openid = $u3['openid'];
										$c_1h_s1 = $u3['s_1_num'];
										$c_1h_fee = $settings['c_1h'];
										$h = 1;
										continue;
									}elseif($u3['is_tuan'] == 2 && $t > 0 && $h == 1 && $h1 == 0 && $i3 > 2){//合伙人间属团队
										$c_2h_uid = $u3['uid'];
										$c_2h_openid = $u3['openid'];
										$c_2h_s1 = $u3['s_1_num'];
										$c_2h_fee = $settings['c_2h'];
										$h = 2;
										continue;
									}elseif($u3['is_tuan'] == 2 && $h == 2 && $h1 == 0 && $i3 > 2){//合伙人直属合伙人
										$c_3h_uid = $u3['uid'];
										$c_3h_openid = $u3['openid'];
										$c_3h_s1 = $u3['s_1_num'];
										$c_3h_fee = $settings['c_3h'];
										$h1 = 1;
										continue;
									}elseif($u3['is_tuan'] == 2 && $h == 2 && $h1 == 1 && $i3 > 2){//合伙人间属合伙人
										$c_4h_uid = $u3['uid'];
										$c_4h_openid = $u3['openid'];
										$c_4h_s1 = $u3['s_1_num'];
										$c_4h_fee = $settings['c_4h'];
										$h1 = 2;
										break;
									}
									$i3++;
								}
								break;
							}elseif($u2['is_tuan'] == 1 && $i2 == 2){//如果是团长间推
								$c_t_uid = $u2['uid'];
								$c_t_openid = $u2['openid'];
								$c_t_s1 = $u2['s_1_num'];
								$c_t_fee = $settings['c_t'];
								$i3 = 1;
								foreach($fa_uid_array as $v3){
									if($settings['system_type'] == 1 && $i3 > 2){
										$u3 = pdo_get(MODULE_NAME . "_user_u", array("uid" => $v3 , "system_id" => $settings['system_id']));
									}elseif($i3 > 2){
										$u3 = pdo_get(MODULE_NAME . "_user", array("uid" => $v3 , "uniacid" => $id));
									}
									if($u3['is_tuan'] == 1 && $t == 0 && $h == 0 && $h1 == 0  && $i3 > 2){//团长直属团长
										$c_2t_uid = $u3['uid'];
										$c_2t_openid = $u3['openid'];
										$c_2t_s1 = $u3['s_1_num'];
										$c_2t_fee = $settings['c_2t'];
										$t = 2;
										continue;
									}elseif($u3['is_tuan'] == 1 && $t == 0 && $h == 0 && $h1 == 0  && $i3 > 2){//团长直属团长
										$c_1h_uid = $u3['uid'];
										$c_1h_openid = $u3['openid'];
										$c_1h_s1 = $u3['s_1_num'];
										$c_1h_fee = $settings['c_1h'];
										$h = 1;
										continue;
									}elseif($u3['is_tuan'] == 2 && $h == 0 && $h1 == 0 && $i3 > 2){//合伙人直属团队
										$c_1h_uid = $u3['uid'];
										$c_1h_openid = $u3['openid'];
										$c_1h_s1 = $u3['s_1_num'];
										$c_1h_fee = $settings['c_1h'];
										$h = 1;
										continue;
									}elseif($u3['is_tuan'] == 2 && $h == 1 && $h1 == 0 && $i3 > 2){//合伙人间属团队
										$c_2h_uid = $u3['uid'];
										$c_2h_openid = $u3['openid'];
										$c_2h_s1 = $u3['s_1_num'];
										$c_2h_fee = $settings['c_2h'];
										$h = 2;
										continue;
									}elseif($u3['is_tuan'] == 2 && $h == 2 && $h1 == 0 && $i3 > 2){//合伙人直属合伙人
										$c_3h_uid = $u3['uid'];
										$c_3h_openid = $u3['openid'];
										$c_3h_s1 = $u3['s_1_num'];
										$c_3h_fee = $settings['c_3h'];
										$h1 = 1;
										continue;
									}elseif($u3['is_tuan'] == 2 && $h == 2 && $h1 == 1 && $i3 > 2){//合伙人间属合伙人
										$c_4h_uid = $u3['uid'];
										$c_4h_openid = $u3['openid'];
										$c_4h_s1 = $u3['s_1_num'];
										$c_4h_fee = $settings['c_4h'];
										$h1 = 2;
										break;
									}
									$i3++;
								}
								break;
							}elseif($u2['is_tuan'] == 2 && $i2 == 2){//如果是合伙人间推
								$c_t_uid = $u2['uid'];
								$c_t_openid = $u2['openid'];
								$c_t_s1 = $u2['s_1_num'];
								$c_t_fee = $settings['c_h_3'];
								$i3 = 1;
								foreach($fa_uid_array as $v3){
									if($settings['system_type'] == 1 && $i3 > 2){
										$u3 = pdo_get(MODULE_NAME . "_user_u", array("uid" => $v3 , "system_id" => $settings['system_id']));
									}elseif($i3 > 2){
										$u3 = pdo_get(MODULE_NAME . "_user", array("uid" => $v3 , "uniacid" => $id));
									}
									if($u3['is_tuan'] == 2 && $h1 == 0 && $i3 > 2){//合伙人直属合伙人
										$c_3h_uid = $u3['uid'];
										$c_3h_openid = $u3['openid'];
										$c_3h_s1 = $u3['s_1_num'];
										$c_3h_fee = $settings['c_3h'];
										$h1 = 1;
										continue;;
									}
									if($u3['is_tuan'] == 2 && $h1 == 1 && $i3 > 2){//合伙人间属合伙人
										$c_4h_uid = $u3['uid'];
										$c_4h_openid = $u3['openid'];
										$c_4h_s1 = $u3['s_1_num'];
										$c_4h_fee = $settings['c_4h'];
										$h1 = 2;
										break;
									}
									$i3++;
								}
								break;
							}
							$i2++;
						}
						break;
					}elseif($u1['is_tuan'] == 1 && $i1 == 1){//如果是团长直推
						$c_2_uid = $u1['uid'];
						$c_2_openid = $u1['openid'];
						$c_2_s1 = $u1['s_1_num'];
						$c_2_fee = $settings['c_t_2'];
						$i2 = 1;
						foreach($fa_uid_array as $v2){
							if($settings['system_type'] == 1){
								$u2 = pdo_get(MODULE_NAME . "_user_u", array("uid" => $v2 , "system_id" => $settings['system_id']));
							}else{
								$u2 = pdo_get(MODULE_NAME . "_user", array("uid" => $v2 , "uniacid" => $id));
							}
							if($u2['is_tuan'] == 1 && $t == 0  && $h == 0 && $h1 == 0 && $i2 > 1){//如果是团长间推
								$c_2t_uid = $u2['uid'];
								$c_2t_openid = $u2['openid'];
								$c_2t_s1 = $u2['s_1_num'];
								$c_2t_fee = $settings['c_2t'];
								$t = 2;
								continue;
							}elseif($u2['is_tuan'] == 2 && $t == 0  && $h == 0 && $h1 == 0 && $i2 > 1){//如果是团长间推
								$c_1h_uid = $u2['uid'];
								$c_1h_openid = $u2['openid'];
								$c_1h_s1 = $u2['s_1_num'];
								$c_1h_fee = $settings['c_1h'];
								$h = 1;
								continue;
							}elseif($u2['is_tuan'] == 2 && $h == 0 && $h1 == 0 && $i2 > 1){//合伙人直属团队
								$c_1h_uid = $u2['uid'];
								$c_1h_openid = $u2['openid'];
								$c_1h_s1 = $u2['s_1_num'];
								$c_1h_fee = $settings['c_1h'];
								$h = 1;
								continue;
							}elseif($u2['is_tuan'] == 2 && $h == 1 && $h1 == 0 && $i2 > 1){//合伙人间属团队
								$c_2h_uid = $u2['uid'];
								$c_2h_openid = $u2['openid'];
								$c_2h_s1 = $u2['s_1_num'];
								$c_2h_fee = $settings['c_2h'];
								$h = 2;
								continue;
							}elseif($u2['is_tuan'] == 2 && $h == 2 && $h1 == 0 && $i2 > 1){//合伙人直属合伙人
								$c_3h_uid = $u2['uid'];
								$c_3h_openid = $u2['openid'];
								$c_3h_s1 = $u2['s_1_num'];
								$c_3h_fee = $settings['c_3h'];
								$h1 = 1;
								continue;
							}elseif($u2['is_tuan'] == 2 && $h == 2 && $h1 == 1 && $i2 > 1){//合伙人间属合伙人
								$c_4h_uid = $u2['uid'];
								$c_4h_openid = $u2['openid'];
								$c_4h_s1 = $u2['s_1_num'];
								$c_4h_fee = $settings['c_4h'];
								$h1 = 2;
								break;
							}
							$i2++;
						}
						break;
					}elseif($u1['is_tuan'] == 2 && $i1 == 1){//如果是合伙人直推
						$c_2_uid = $u1['uid'];
						$c_2_openid = $u1['openid'];
						$c_2_s1 = $u1['s_1_num'];
						$c_2_fee = $settings['c_h_2'];
						$i2 = 1;
						foreach($fa_uid_array as $v2){
							if($settings['system_type'] == 1 && $i2 > 1){
								$u2 = pdo_get(MODULE_NAME . "_user_u", array("uid" => $v2 , "system_id" => $settings['system_id']));
							}elseif($i2 > 1){
								$u2 = pdo_get(MODULE_NAME . "_user", array("uid" => $v2 , "uniacid" => $id));
							}
							if($u2['is_tuan'] == 2 && $h1 == 0 && $i2 > 1){//合伙人直属合伙人
								$c_3h_uid = $u2['uid'];
								$c_3h_openid = $u2['openid'];
								$c_3h_s1 = $u2['s_1_num'];
								$c_3h_fee = $settings['c_3h'];
								$h1 = 1;
								continue;
							}elseif($u2['is_tuan'] == 2 && $h1 == 1 && $i2 > 1){//合伙人间属合伙人
								$c_4h_uid = $u2['uid'];
								$c_4h_openid = $u2['openid'];
								$c_4h_s1 = $u2['s_1_num'];
								$c_4h_fee = $settings['c_4h'];
								$h1 = 2;
								break;
							}
							$i2++;
						}
						break;
					}
					$i1++;
				}
			}
		}elseif($u['is_tuan'] == 1){//如果是团长
			$c_1_uid = $u['uid'];
			$c_1_openid = $u['openid'];
			$c_1_s1 = $u['s_1_num'];
			$c_1_fee = $settings['c_t_1'];
			if($u['invite_uid'] != '0'){
				$fa_uid_array = array_reverse(explode("_",$u['invite_uid']));
				$i1 = 1;
				foreach($fa_uid_array as $v){
					if($settings['system_type'] == 1){
						$u1 = pdo_get(MODULE_NAME . "_user_u", array("uid" => $v , "system_id" => $settings['system_id']));
					}else{
						$u1 = pdo_get(MODULE_NAME . "_user", array("uid" => $v , "uniacid" => $id));
					}
					if($u1['is_tuan'] == 1 && $t == 0 && $h == 0 && $h1 == 0 && $i1 == 1){//如果是团长直推
						$c_2t_uid = $u1['uid'];
						$c_2t_openid = $u1['openid'];
						$c_2t_s1 = $u1['s_1_num'];
						$c_2t_fee = $settings['c_2t'];
						$t = 2;
						continue;
					}elseif($u1['is_tuan'] == 2 && $t == 0 && $h == 0 && $h1 == 0 && $i1 == 1){//如果是合伙人直推
						$c_1h_uid = $u1['uid'];
						$c_1h_openid = $u1['openid'];
						$c_1h_s1 = $u1['s_1_num'];
						$c_1h_fee = $settings['c_1h'];
						$h = 1;
						continue;
					}elseif($u1['is_tuan'] == 2 && $h == 0 && $h1 == 0 && $i1 > 1){//合伙人直属团队
						$c_1h_uid = $u1['uid'];
						$c_1h_openid = $u1['openid'];
						$c_1h_s1 = $u1['s_1_num'];
						$c_1h_fee = $settings['c_1h'];
						$h = 1;
						echo $c_1h_uid;
						continue;
					}elseif($u1['is_tuan'] == 2 && $h == 1 && $h1 == 0 && $i1 > 1){//合伙人间属团队
						$c_2h_uid = $u1['uid'];
						$c_2h_openid = $u1['openid'];
						$c_2h_s1 = $u1['s_1_num'];
						$c_2h_fee = $settings['c_2h'];
						$h = 2;
						echo $c_2h_uid;
						continue;
					}elseif($u1['is_tuan'] == 2 && $h == 2 && $h1 == 0 && $i1 > 1){//合伙人直属合伙人
						$c_3h_uid = $u1['uid'];
						$c_3h_openid = $u1['openid'];
						$c_3h_s1 = $u1['s_1_num'];
						$c_3h_fee = $settings['c_3h'];
						$h1 = 1;
						continue;
					}elseif($u1['is_tuan'] == 2 && $h == 2 && $h1 == 1 && $i1 > 1){//合伙人间属合伙人
						$c_4h_uid = $u1['uid'];
						$c_4h_openid = $u1['openid'];
						$c_4h_s1 = $u1['s_1_num'];
						$c_4h_fee = $settings['c_4h'];
						$h1 = 2;
						break;
					}
					$i1++;
				}
			}
		}elseif($u['is_tuan'] == 2){//如果是合伙人
			$c_1_uid = $u['uid'];
			$c_1_openid = $u['openid'];
			$c_1_s1 = $u['s_1_num'];
			$c_1_fee = $settings['c_h_1'];
			if($u['invite_uid'] != '0'){
				$fa_uid_array = array_reverse(explode("_",$u['invite_uid']));
				$i1 = 1;
				foreach($fa_uid_array as $v){
					if($settings['system_type'] == 1){
						$u1 = pdo_get(MODULE_NAME . "_user_u", array("uid" => $v , "system_id" => $settings['system_id']));
					}else{
						$u1 = pdo_get(MODULE_NAME . "_user", array("uid" => $v , "uniacid" => $id));
					}
					if($u1['is_tuan'] == 2 && $h1 == 0 && $i1 > 0){//合伙人直属合伙人
						$c_3h_uid = $u1['uid'];
						$c_3h_openid = $u1['openid'];
						$c_3h_s1 = $u1['s_1_num'];
						$c_3h_fee = $settings['c_3h'];
						$h1 = 1;
						continue;
					}elseif($u1['is_tuan'] == 2 && $h1 == 1 && $i1 > 0){//合伙人间属合伙人
						$c_4h_uid = $u1['uid'];
						$c_4h_openid = $u1['openid'];
						$c_4h_s1 = $u1['s_1_num'];
						$c_4h_fee = $settings['c_4h'];
						$h1 = 2;
						break;
					}
					$i1++;
				}
			}
		}
	}
	$c1_uid = $c_1_uid;
	if(!$c_1_uid || !$c_1_fee){
		$c_1_uid = 0;
		$c_1_openid = '';
		$c_1_s1 = 0;
		$c_1_fee = 0;
	}
	if(!$c_2_uid || !$c_2_fee){
		$c_2_uid = 0;
		$c_2_openid = '';
		$c_2_s1 = 0;
		$c_2_fee = 0;
	}
	if(!$c_3_uid || !$c_3_fee){
		$c_3_uid = 0;
		$c_3_openid = '';
		$c_3_s1 = 0;
		$c_3_fee = 0;
	}
	if(!$c_t_uid || !$c_t_fee){
		$c_t_uid = 0;
		$c_t_openid = '';
		$c_t_s1 = 0;
		$c_t_fee = 0;
	}
	if(!$c_2t_uid || !$c_2t_fee){
		$c_2t_uid = 0;
		$c_2t_openid = '';
		$c_2t_s1 = 0;
		$c_2t_fee = 0;
	}
	if(!$c_1h_uid || !$c_1h_fee){
		$c_1h_uid = 0;
		$c_1h_openid = '';
		$c_1h_s1 = 0;
		$c_1h_fee = 0;
	}
	if(!$c_2h_uid || !$c_2h_fee){
		$c_2h_uid = 0;
		$c_2h_openid = '';
		$c_2h_s1 = 0;
		$c_2h_fee = 0;
	}
	if(!$c_3h_uid || !$c_3h_fee){
		$c_3h_uid = 0;
		$c_3h_openid = '';
		$c_3h_s1 = 0;
		$c_3h_fee = 0;
	}
	if(!$c_4h_uid || !$c_4h_fee){
		$c_4h_uid = 0;
		$c_4h_openid = '';
		$c_4h_s1 = 0;
		$c_4h_fee = 0;
	}
	$fee = array(
		'c1_uid' => $c1_uid,
		'c_1_uid' => $c_1_uid,
		'c_1_openid' => $c_1_openid,
		'c_1_s1' => $c_1_s1,
		'c_1_fee' => $c_1_fee,
		'c_2_uid' => $c_2_uid,
		'c_2_openid' => $c_2_openid,
		'c_2_s1' => $c_2_s1,
		'c_2_fee' => $c_2_fee,
		'c_3_uid' => $c_3_uid,
		'c_3_openid' => $c_3_openid,
		'c_3_s1' => $c_3_s1,
		'c_3_fee' => $c_3_fee,
		'c_t_uid' => $c_t_uid,
		'c_t_openid' => $c_t_openid,
		'c_t_s1' => $c_t_s1,
		'c_t_fee' => $c_t_fee,
		'c_2t_uid' => $c_2t_uid,
		'c_2t_openid' => $c_2t_openid,
		'c_2t_s1' => $c_2t_s1,
		'c_2t_fee' => $c_2t_fee,
		'c_1h_uid' => $c_1h_uid,
		'c_1h_openid' => $c_1h_openid,
		'c_1h_s1' => $c_1h_s1,
		'c_1h_fee' => $c_1h_fee,
		'c_2h_uid' => $c_2h_uid,
		'c_2h_openid' => $c_2h_openid,
		'c_2h_s1' => $c_2h_s1,
		'c_2h_fee' => $c_2h_fee,
		'c_3h_uid' => $c_3h_uid,
		'c_3h_openid' => $c_3h_openid,
		'c_3h_s1' => $c_3h_s1,
		'c_3h_fee' => $c_3h_fee,
		'c_4h_uid' => $c_4h_uid,
		'c_4h_openid' => $c_4h_openid,
		'c_4h_s1' => $c_4h_s1,
		'c_4h_fee' => $c_4h_fee
	);
	return $fee;
	
}


function refund1($refund_id) {
	load()->classs('pay');
	global $_W;
	$refundlog = pdo_get('core_refundlog', array('id' => $refund_id));
	$params = array('uniontid' => $refundlog['uniontid']);
	$params['uniacid'] = $refundlog['is_wish'] == 1 ? $refundlog['uniacid'] : $_W['uniacid'];
	$paylog = pdo_get('core_paylog', $params);
	if ($paylog['type'] == 'wechat' || $paylog['type'] == 'wxapp') {
		$refund_param = reufnd_wechat_build1($refund_id, $refundlog['is_wish']);
		if (is_error($refund_param)) {
			return $refund_param;
		}
		if ($refundlog['is_wish'] == 1) {
			$module = 'store';
			$cert_file = ATTACHMENT_ROOT . 'store_wechat_refund_all.pem';
		} else {
			$module = '';
			$cert_file = ATTACHMENT_ROOT . $_W['uniacid'] . '_wechat_refund_all.pem';
		}

		$wechat = Pay::create('wechat', $module);
		$response = $wechat->refund($refund_param, $module);
		unlink($cert_file);
		if (is_error($response)) {
			pdo_update('core_refundlog', array('status' => '-1'), array('id' => $refund_id));
			return $response;
		} else {
			return $response;
		}
	} elseif ($paylog['type'] == 'alipay') {
		$refund_param = reufnd_ali_build1($refund_id, $refundlog['is_wish']);
		if (is_error($refund_param)) {
			return $refund_param;
		}
		$module = $refundlog['is_wish'] == 1 ? 'store' : '';
		$ali = Pay::create('alipay', $module);
		$response = $ali->refund($refund_param, $refund_id);
		if (is_error($response)) {
			pdo_update('core_refundlog', array('status' => '-1'), array('id' => $refund_id));
			return $response;
		} else {
			return $response;
		}
	}
	return error(1, '此订单退款方式不存在');
}

function reufnd_wechat_build1($refund_id, $is_wish = 0) {
	global $_W;
	if ($is_wish == 1) {
		$setting = setting_load('store_pay');
		$pay_setting = $setting['store_pay'];
		$refund_setting = $setting['store_pay']['wechat_refund'];
	} else {
		$setting = uni_setting_load('payment', $_W['uniacid']);
		$pay_setting = $setting['payment'];
		$refund_setting = $setting['payment']['wechat_refund'];
	}

	if ($refund_setting['switch'] != 1) {
		return error(1, '未开启微信退款功能！');
	}
	if (empty($refund_setting['key']) || empty($refund_setting['cert'])) {
		return error(1, '缺少微信证书！');
	}

	$refundlog = pdo_get('core_refundlog', array('id' => $refund_id));
	$uniacid = $is_wish == 1 ? $refundlog['uniacid'] : $_W['uniacid'];
	$paylog = pdo_get('core_paylog', array('uniacid' => $uniacid, 'uniontid' => $refundlog['uniontid']));
	$account = uni_fetch($uniacid);
	$refund_param = array(
		'appid' => $is_wish == 1 ? $pay_setting['wechat']['appid'] : $account['key'],
		'mch_id' => $pay_setting['wechat']['mchid'],
		'out_trade_no' => $refundlog['uniontid'],
		'out_refund_no' => $refundlog['refund_uniontid'],
		'total_fee' => $paylog['card_fee'] * 100,
		'refund_fee' => $refundlog['fee'] * 100,
		'nonce_str' => random(8),
		'refund_desc' => $refundlog['reason']
	);

	$cert = authcode($refund_setting['cert'], 'DECODE');
	$key = authcode($refund_setting['key'], 'DECODE');

	$cert_file = $is_wish == 1 ? 'store_wechat_refund_all.pem' : $_W['uniacid'] . '_wechat_refund_all.pem';
	file_put_contents(ATTACHMENT_ROOT . $cert_file, $cert . $key);
	return $refund_param;
}